package com.ts.api.module.match.market.internal.valid.handler;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Empty;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.context.ChannelContext;
import com.ts.api.common.context.ChannelRequestContext;
import com.ts.api.common.localcache.DistrictCache;
import com.ts.api.common.util.OpenTimeUtils;
import com.ts.api.module.channel.dao.ChannelMarketDao;
import com.ts.api.module.channel.entity.ChannelRequest;
import com.ts.api.module.channel.entity.po.ChannelMarketPO;
import com.ts.api.module.channel.entity.po.ChannelPO;
import com.ts.api.module.channel.service.ChannelConfigService;
import com.ts.api.module.common.entity.DistrictPO;
import com.ts.api.module.common.entity.bo.IpInfoBO;
import com.ts.api.module.common.service.IpInfoService;
import com.ts.api.module.match.market.internal.valid.model.ChannelMarketValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  渠道贷超验证器
 *
 *  @author Pan
 */
@Component
public class ChannelMarketValidatorHandler {

    @Autowired
    private ChannelMarketDao channelMarketDao;
    @Autowired
    private IpInfoService ipInfoService;
    @Autowired
    private ChannelConfigService channelConfigService;

    /**
     *  返回业务逻辑验证通过的的渠道贷超列表
     *
     *  @author Pan
     *  @return ChannelMarketValidationResult   返回业务验证通过的贷超对象
     */
    public ChannelMarketValidationResult valid() {
        ChannelPO channelPO = ChannelContext.get();
        ChannelRequest channelRequest = ChannelRequestContext.get();

        //  返回业务验证成功对象
        ChannelMarketValidationResult channelMarketValidationResult = new ChannelMarketValidationResult();
        channelMarketValidationResult.setChannelPO(channelPO);
        channelMarketValidationResult.setChannelRequest(channelRequest);

        //  符合IP屏蔽 不显示贷超列表
        if (channelConfigService.hasIpOffModule()) {
            channelMarketValidationResult.setListMarketValidate(Empty.list());
            return channelMarketValidationResult;
        }

        //  获取原始渠道贷超列表
        List<ChannelMarketPO> listChannelMarket = channelMarketDao.list(channelPO.getId());
        channelMarketValidationResult.setListMarketOriginal(listChannelMarket);

        if (ValidParam.isEmpty(listChannelMarket)) {
            channelMarketValidationResult.setListMarketValidate(Empty.list());
            return channelMarketValidationResult;
        }

        //  拷贝原始对象进行过滤
        List<ChannelMarketPO> listChannelMarketCopy = CollUtils.copy(listChannelMarket);

        //  城市区域过滤
        filterByArea(listChannelMarketCopy);
        //  开放时间过滤
        List<ChannelMarketPO> filterByOpenTime = filterByOpenTime(listChannelMarketCopy);

        channelMarketValidationResult.setListMarketValidate(filterByOpenTime);
        return channelMarketValidationResult;
    }

    /**
     *  城市区域过滤
     *
     *  @author Pan
     *  @param  listChannelMarket   渠道贷超对象
     *  @return List
     */
    private void filterByArea(List<ChannelMarketPO> listChannelMarket) {
        //  根据当前贷超-获取存在城市的贷超列表
        List<Long> existMarketArea = channelMarketDao.listMarketAreaByMarketId(CollUtils.toList(listChannelMarket, t -> t.getId()));
        if (ValidParam.isNotEmpty(existMarketArea)) {
            //  获取ip地区
            String ip = ChannelRequestContext.get().getIp();
            IpInfoBO ipInfoBO = ipInfoService.getByIp(ip);
            String cityName = ipInfoBO.getCity();
            DistrictPO districtPO = DistrictCache.getByName(cityName);

            //  过滤地区
            if (ValidParam.isNotNull(districtPO)) {
                List<Long> listMarketIds = channelMarketDao.listMarketAreaByAreaCode(districtPO.getCode());
                CollUtils.filter(listChannelMarket, t -> {
                    Long findId = CollUtils.findOne(listMarketIds, marketId -> Eq.object(t.getId(), marketId));
                    return ValidParam.isNotNull(findId);
                });
            }
        }
    }

    /**
     *  开放时间过滤
     *
     *  @author Pan
     *  @param  listChannelMarket   渠道贷超对象
     *  @return List
     */
    private List<ChannelMarketPO> filterByOpenTime(List<ChannelMarketPO> listChannelMarket) {
        return OpenTimeUtils.filter(listChannelMarket);
    }
}
